package com.example.ytr.recyclerview_104ytr.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;


import com.example.ytr.recyclerview_104ytr.R;
import com.example.ytr.recyclerview_104ytr.adapter.MyAdapter;
import com.example.ytr.recyclerview_104ytr.bean.ArticleBean;
import com.example.ytr.recyclerview_104ytr.iface.ArticleListener;
import com.example.ytr.recyclerview_104ytr.model.ArticleModel;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;//RecyclerView控件
    private RecyclerView.LayoutManager layoutManager;//RecyclerView布局管理
    private MyAdapter myAdapter;//RecyclerView的适配器
    private List<ArticleBean> list = null;//保存数据

    ArticleListener articleListener = new ArticleListener() {

        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            list = beanlist;
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
        }


    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();
        ArticleModel model = new ArticleModel();
        model.getResultList("article", 1, "rv1l2tidbomann2qn86nln6ur2", articleListener);
    }

    //初始化
    private void initRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        layoutManager = new LinearLayoutManager(this);//创建线性布局
        recyclerView.setLayoutManager(layoutManager);//设置布局管理器
        recyclerView.setHasFixedSize(true);//固定每个item高度，可以提高性能
        //list=getData();//获取数据
        myAdapter = new MyAdapter(this);
        myAdapter.setList(list);//传进数据
        recyclerView.setAdapter(myAdapter);//绑定Recyclerview和Adapter

    }
}